package com.book.controller;

import com.book.entity.Pager;
import com.book.entity.Student;
import com.book.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Created by dell on 2017/6/14.
 */
@Controller
public class StudentController {
    static int pageSize = 10;
    @Autowired
    StudentService studentService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String studentList(Student student, Model model) {
        Student student1 = studentService.login(student.getStudentName(), student.getStudentPassword());
        if (student1.getStudentId() != null) {
            model.addAttribute("loginUser", student1);
        }
        return "redirect:/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request) {
        Pager pager = new Pager();
        pager.setPageSize(pageSize);
        pager.setTotalRecord(studentService.selectCount());
        int nums = pager.getTotalRecord() % pageSize;
        int AllPage = pager.getTotalRecord() / pageSize;
        if (nums == 0) {
            int[] arr1 = new int[AllPage];
            for (int i = 0; i < AllPage; i++) {
                arr1[i] = i;
            }
            pager.setAllPages(AllPage);
            pager.setAllPage(arr1);
        } else {
            AllPage = AllPage + 1;
            int[] arr1 = new int[AllPage];
            for (int i = 0; i < AllPage; i++) {
                arr1[i] = i;
            }
            pager.setAllPages(AllPage);
            pager.setAllPage(arr1);
        }

        if (request.getParameter("Pageindex") != null) {
            System.out.println("获得的页数" + request.getParameter("Pageindex"));
            String indexpage = request.getParameter("Pageindex");
            int index = Integer.valueOf(indexpage);
            pager.setPageIndex(index);
        } else {

        }
        pager.setDatas(studentService.selectPageList(pager.getPageIndex() * pageSize, 10));
        model.addAttribute("page", pager);
        return "user/list";
    }


    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Model model, Student student) {


        return "user/add";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addp(Model model, Student student) {
        int n = studentService.insert(student);
        if (n > 0) {
            return "redirect:/list";
        } else {
            return "添加失败";
        }
    }

    @RequestMapping(value = "{studentId}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable String studentId) {

        System.out.println("=======================收到的id" + studentId);
        int n = studentService.deleteByPrimaryKey(Integer.valueOf(studentId));
        if (n > 0) {
            return "redirect:/list";
        } else {
            return "删除失败";
        }

    }


    @RequestMapping(value = "{studentId}/update", method = RequestMethod.GET)
    public String update(@PathVariable String studentId, Model model, Student student) {
        System.out.println("=======================收到的id" + studentId);
        student.setStudentId(Integer.valueOf(studentId));
        model.addAttribute("updateStudent", studentService.selectByPrimaryKey(student));

        return "user/update";


    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updatep(Student student, Model model) {
        System.out.println("收到的值" + student.getStudentName());

        int n = studentService.updateByPrimaryKey(student);

        if (n > 0) {
            model.addAttribute("student", studentService.selectByPrimaryKey(student));
            return "user/userinfo";
        } else {
            return "修改失败";
        }

    }
}
